package com.M10.BE.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.M10.BE.R;
import com.M10.BE.fragment.FrgInventory;
import com.M10.BE.fragment.FrgList;
import com.M10.BE.fragment.FrgStatistics;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.PageBottomTabLayout;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;

public class MenuActivity extends FragmentActivity {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.fl_content)
    FrameLayout flContent;
    @BindView(R.id.pt_menu)
    PageBottomTabLayout ptMenu;
    private FrgList frgList = null;
    private FrgStatistics frgStatistics=null;
    private FrgInventory frgInventory = null;
    private NavigationController mNavigationController;
    OnTabItemSelectedListener listener = new OnTabItemSelectedListener() {
        @Override
        public void onSelected(int index, int old) {
            tvTitle.setText(index + "首页");
            switch (index) {
                case 0:
                    if (frgList == null) {
                        frgList = FrgList.newInstance();
                        // 判断当前界面是否隐藏，如果隐藏就进行添加显示，false表示显示，true表示当前界面隐藏
                        addFragment(frgList);
                        showFragment(frgList);
                    } else {
                        if (frgList.isHidden()) {
                            showFragment(frgList);
                        }
                    }
                    break;
                case 1:
                    if (frgStatistics == null) {
                        frgStatistics = FrgStatistics.newInstance();
                        // 判断当前界面是否隐藏，如果隐藏就进行添加显示，false表示显示，true表示当前界面隐藏
                        addFragment(frgStatistics);
                        showFragment(frgStatistics);
                    } else {
                        if (frgStatistics.isHidden()) {
                            showFragment(frgStatistics);
                        }
                    }
                    break;
                case 2:
                    if (frgInventory == null) {
                        frgInventory = FrgInventory.newInstance();
                        // 判断当前界面是否隐藏，如果隐藏就进行添加显示，false表示显示，true表示当前界面隐藏
                        addFragment(frgInventory);
                        showFragment(frgInventory);
                    } else {
                        if (frgInventory.isHidden()) {
                            showFragment(frgInventory);
                        }
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onRepeat(int index) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_menu);
        ButterKnife.bind(this);

        initTab();
    }

    private void initTab() {
        if (frgList == null) {
            frgList = FrgList.newInstance();
            // 判断当前界面是否隐藏，如果隐藏就进行添加显示，false表示显示，true表示当前界面隐藏
            addFragment(frgList);
            showFragment(frgList);
        } else {
            if (frgList.isHidden()) {
                showFragment(frgList);
            }
        }
        mNavigationController = ptMenu.material()
                .addItem(R.drawable.list_grayone, R.drawable.list_blueone, "列表",
                        ContextCompat.getColor(this, R.color.tab))
                .addItem(R.drawable.list_graytwo, R.drawable.list_bluetwo, "统计",
                        ContextCompat.getColor(this, R.color.tab))
                .addItem(R.drawable.list_graythree, R.drawable.list_bluethree, "盘点",
                        ContextCompat.getColor(this, R.color.tab))
                //  .setDefaultColor(Color.BLACK)//未选中状态的颜色
                // .setMode( MaterialMode.CHANGE_BACKGROUND_COLOR)//这里可以设置样式模式，总共可以组合出4种效果
                .build();
        mNavigationController.addTabItemSelectedListener(listener);
        ptMenu.setBackgroundColor(Color.WHITE);
    }

    /**
     * 添加Fragment
     **/
    public void addFragment(Fragment fragment) {
        FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
        ft.add(R.id.fl_content, fragment);
        ft.commit();
    }

    /**
     * 显示Fragment
     **/
    public void showFragment(Fragment fragment) {
        FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
        // 设置Fragment的切换动画
        // ft.setCustomAnimations(R.anim.cu_push_right_in,
        // R.anim.cu_push_left_out);
        if (frgList != null) {
            ft.hide(frgList);
        }
        if (frgInventory != null) {
            ft.hide(frgInventory);
        }
        if (frgStatistics != null) {
            ft.hide(frgStatistics);
        }

        ft.show(fragment);
        ft.commitAllowingStateLoss();
    }

}
